import time

from selenium.webdriver import Chrome

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
def piaofang_movie():
    print('开始抓取2021电影排行榜')
    opt = Options()
    opt.add_argument('--headless')
    opt.add_argument('--disable gpu')

    chrome = Chrome(options=opt)
    url = 'https://www.endata.com.cn/BoxOffice/BO/Year/index.html'
    chrome.get(url)
    time.sleep(1)
    f = open('movies.csv', mode='a',encoding='utf-8')
    select_el = chrome.find_element(By.ID,'OptionDate')
    # 找到切换年份对应的element
    select = Select(select_el)

    select.select_by_index(1)
    # 用索引的方式进行切换页面
    time.sleep(2)
    tr_list = chrome.find_elements(By.XPATH,'//table[@class="bo-table img-table"]//tr')[1:]
    for tr in tr_list:
        for td in tr.find_elements(By.XPATH,'./td'):
            time.sleep(0.5)
            f.write(td.text.strip())
            # print(td.text.strip())
            f.write(',')
        f.write('\n')
    f.write('\n\n')
    print('2021电影票房排行榜抓取完毕')



